# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.common.exceptions import TimeoutException
from selenium.webdriver.support.ui import WebDriverWait # available since 2.4.0
from selenium.webdriver.support import expected_conditions as EC # available since 2.26.0
from selenium.webdriver.common.action_chains import ActionChains
import unittest
import random
import time
from cookies import lasthopes, randomword
import re
import string
import sys
from selenium.webdriver.common.keys import Keys
from PIL import Image
from os import remove, listdir, sep
from os.path import basename, isfile
import tempfile
from warnings import warn
from basecart import BaseCart

class cart_add_ad(BaseCart):
    def test_cart_add_ad(self):
        driver = self.driver
        #Phones always have ads
        self.get_url("/category/mobiles.html")
        self.get_item()
        #Wait for checkboxes appeared
        WebDriverWait(driver, timeout=10).until(lambda dr: dr\
                                  .find_element_by_css_selector("div.b-card-box-item-plus"))
        cart_btn = driver.find_element_by_css_selector("p.b-header-top-cart-title")
        ads_block = driver.find_element_by_css_selector("div.b-card-related")
        ads_btn = ads_block.find_element_by_css_selector("span.b-sbutton.set_system.scheme_blue.size_1.js-card-kits-order")
        cbxs = len([a for a in WebDriverWait(ads_block, timeout=10).until(lambda dr: dr\
                        .find_elements_by_css_selector("div.b-card-box-item-plus"))\
                        if a.is_displayed])
        items_before = self.cart_items_ontop()
        ads_btn.click()
        #Check for items appeared in cart
        WebDriverWait(driver, timeout=10).until(lambda dr: self.cart_items_ontop()>items_before)
        #Check of count items in cart
        #assert self.cart_items_ontop() == cbxs+1,\
        #'Count of items in cart\"'+str(self.cart_items_ontop())+'\" not equals '+str(cbxs+1)+' of additions and main item.'
